package com.iloomo.controller.core;

import java.util.Map;
import java.util.List;
import javax.annotation.Resource;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.iloomo.controller.BaseController;
import com.iloomo.util.AppUtil;
import com.iloomo.util.PageData;
import com.iloomo.util.PagerModel;
import org.apache.commons.lang.StringUtils;
import com.iloomo.service.core.KeyValueManager;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 说明：表keyValue的控制层
 * 创建人：system
 * 创建时间：2020-02-13
 */
@Controller
@RequestMapping(value="/core/keyValue")
public class KeyValueController extends BaseController {

	@Resource(name="keyValueService")
	private KeyValueManager keyValueService;

	/**保存
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/save",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "保存", notes = "保存数据")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object save(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try {

			int row = keyValueService.save(pd);
			if(row <= 0){
				throw new IllegalArgumentException("保存失败!");
			}
			json.put("id",pd.get("id"));
			msg = "保存成功！";
			flag = true;
		}catch(IllegalArgumentException e){
			msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/del",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "删除", notes = "根据id删除")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object del(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try {
            String id = pd.getString("id");
            String[] idList=id.split(",");
            for (int i=0;i<idList.length;i++) {
                pd.put("id",idList[i]);
                if(StringUtils.isBlank(id)){
                    throw new IllegalArgumentException("操作ID不能为空！");
                }
                int row = keyValueService.deleteById(pd);
                if(row<=0){
                    throw new IllegalArgumentException("删除失败!");
                }
                msg = "删除成功!";
                flag = true;
            }
		}catch(IllegalArgumentException e){
					msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/update",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "更新数据", notes = "根据id")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object update(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try {
			String id = pd.getString("id");
			if(StringUtils.isBlank(id)){
				throw new IllegalArgumentException("修改ID不能为空！");
			}

		String[] idList=id.split(",");
		for (int i=0;i<idList.length;i++) {
						pd.put("id",idList[i]);
			if(StringUtils.isBlank(id)){
				throw new IllegalArgumentException("操作ID不能为空！");
			}
		int row = keyValueService.updateById(pd);
			if(row<=0){
			throw new IllegalArgumentException("修改失败!");
			}
			msg = "修改成功!";
			flag = true;
		}

		}catch(IllegalArgumentException e){
			msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**根据id获取信息
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/findById",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "查询数据", notes = "根据id")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object findById(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try {
			String id = pd.getString("id");
			if(StringUtils.isBlank(id)){
				throw new IllegalArgumentException("查询ID不能为空！");
			}
			Map keyValue = keyValueService.findById(pd);
			if(null == keyValue){
				throw new IllegalArgumentException("数据不存在");
			}
			msg = "查询成功!";
			json.put("rows", keyValue);
			flag = true;
		}catch(IllegalArgumentException e){
			msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**分页列表
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/list",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "获得数据列表", notes = "根据查询条件")
	@ApiImplicitParams({
	@ApiImplicitParam(	name="page" ,value = "第几页" ) ,
	@ApiImplicitParam(	name="rows" ,value = "每页多少条数据" ) ,
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object list(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try{
			PagerModel pm = new PagerModel(pd);
			pd.put("offset", pm.getOffset());
			int pageSize = pm.getPageSize();
			pd.put("pageSize", pageSize);

			pm = keyValueService.list(pd);
			// 计算总页数
			pm.setPagerSize((pm.getTotal() + pm.getPageSize() - 1 )/ pm.getPageSize());
			msg = "查询成功!";
			json.put("page", pm.getPageNo());
			json.put("total", pm.getPagerSize());
			json.put("records", pm.getTotal());
			json.put("rows", pm.getList());
			flag = true;
		}catch(NumberFormatException e){
			msg = "请检查传递参数的数据类型是否正确！";
		}catch(IllegalArgumentException e){
			msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**全部列表
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/listAll",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "获得全部数据", notes = "根据查询条件")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object listAll(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try{
			List<Map> list = keyValueService.listAll(pd);
			msg = "查询成功!";
			json.put("rows", list);
			flag = true;
		}catch(NumberFormatException e){
			msg = "请检查传递参数的数据类型是否正确！";
		}catch(IllegalArgumentException e){
			msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

    /**批量保存，传入的参数是一个json数组，前端的json需要用单引号“’”
    * @param
    * @throws Exception
    */
    @RequestMapping(value="/batchSave",produces="application/json;charset=UTF-8")
    @ResponseBody

    @ApiOperation(httpMethod="POST" ,  value = "保存", notes = "保存数据")
    @ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
    })
	public Object batchSave(){
	    PageData pd = new PageData();
	    pd = this.getPageData();
	    JSONObject json = new JSONObject();
	    //默认将返回结果设置为false，后面正常结束的时候，设置为true
	    boolean flag = false;
	    String msg = "";
	    try {
	    List list = pd.getBatchParam();
	        Object ret = keyValueService.batchSave(list);
	    if(ret == null){
	        throw new IllegalArgumentException("保存失败!");
    	}
	        json.put("id",pd.get("id"));
	    msg = "保存成功！";
	        flag = true;
	    }catch(IllegalArgumentException e){
    	    msg = e.getMessage();
    	}catch(Exception e){
	        logger.error(this.getClass().getName()+"处理异常",e);
        	msg = "后台处理异常！";
    	}
	    json.put("msg", msg);
	    json.put("success", flag);
	    return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**分页列表支持模糊查询支持多字段or
	* @param
	* @throws Exception
	*/
	@RequestMapping(value="/listWithLike",produces="application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod="POST" ,  value = "获得数据列表", notes = "根据查询条件")
	@ApiImplicitParams({
	@ApiImplicitParam(	name="page" ,value = "第几页" ) ,
	@ApiImplicitParam(	name="rows" ,value = "每页多少条数据" ) ,
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object listWithLike(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try{
			PagerModel pm = new PagerModel(pd);
			pd.put("offset", pm.getOffset());
			int pageSize = pm.getPageSize();
			pd.put("pageSize", pageSize);

			pm = keyValueService.listWithLike(pd);
		// 计算总页数
			pm.setPagerSize((pm.getTotal() + pm.getPageSize() - 1 )/ pm.getPageSize());
			msg = "查询成功!";
			json.put("page", pm.getPageNo());
			json.put("total", pm.getPagerSize());
			json.put("records", pm.getTotal());
			json.put("rows", pm.getList());
			flag = true;
		}catch(NumberFormatException e){
			msg = "请检查传递参数的数据类型是否正确！";
		}catch(IllegalArgumentException e){
			msg = e.getMessage();
		}catch(Exception e){
			logger.error(this.getClass().getName()+"处理异常",e);
			msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	/**全部列表支持模糊查询支持多字段or
	* @param
	* @throws Exception
	*/
	@RequestMapping(value="/listAllWithLike",produces="application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation(httpMethod="POST" ,  value = "模糊查询", notes = "根据查询条件")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object listAllWithLike(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try{
			List<Map> list = keyValueService.listAllWithLike(pd);
				msg = "查询成功!";
				json.put("rows", list);
				flag = true;
			}catch(NumberFormatException e){
				msg = "请检查传递参数的数据类型是否正确！";
			}catch(IllegalArgumentException e){
				msg = e.getMessage();
			}catch(Exception e){
				logger.error(this.getClass().getName()+"处理异常",e);
				msg = "后台处理异常！";
			}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}

	@RequestMapping(value = "/selectCount", produces = "application/json;charset=UTF-8")
	@ResponseBody

	@ApiOperation(httpMethod = "POST", value = "查询", notes = "查询数据")
	@ApiImplicitParams({
		@ApiImplicitParam(	name="id" ,value = "|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="name" ,value = "来源名称|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="type" ,value = "Type数不能重复,Type相同时value值不能相同|int",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="value" ,value = "|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="add_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="update_time" ,value = "|timestamp",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="is_delete" ,value = "是否删除，0为显示，1为删除|varchar",paramType="query",required=false,dataType="String" ), 	
		@ApiImplicitParam(	name="resources_id" ,value = "公共资源的id|int",paramType="query",required=false,dataType="String" ), 
	})
	public Object selectCount(){
		PageData pd = new PageData();
		pd = this.getPageData();
		JSONObject json = new JSONObject();
		//默认将返回结果设置为false，后面正常结束的时候，设置为true
		boolean flag = false;
		String msg = "";
		try {
		int count = keyValueService.selectCount(pd);
		json.put("count",count);
		msg = "查询成功！";
		flag = true;
		}
		catch(NumberFormatException e){
		msg = "请检查传递参数的数据类型是否正确！";
		}catch(IllegalArgumentException e){
		msg = e.getMessage();
		}catch(Exception e){
		logger.error(this.getClass().getName()+"处理异常",e);
		msg = "后台处理异常！";
		}
		json.put("msg", msg);
		json.put("success", flag);
		return AppUtil.getCallbackJson(pd, json.toString(), true);
	}
}
